package com.zhuss.product.processor;

import java.util.Date;

import com.zhuss.circle.IOrderService;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.processor.cache.StrategyTrainCache;
import com.zhuss.product.processor.cache.TrainCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.train.DeleteTrainBo;
import com.zhuss.bean.vo.product.train.DetailTrainVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.exception.DzcxException;
import com.zhuss.product.db.dao.ext.TrainDao;
import com.zhuss.product.db.entity.Train;
import com.zhuss.util.JsonUtil;

/**
 * @description: 车次删除
 * @author: zhushanshuo
 * @create: Mon Jan 04 08:55:12 CST 2021
 **/
@Component("trainDelete")
public class TrainDelete extends AbstractProcessor<Object, DeleteTrainBo> implements Processor<Object, DeleteTrainBo> {

    @Autowired
    private TrainDao trainDao;

    @Autowired
	@Qualifier("trainRead")
	private TrainRead trainRead;

	@Autowired
	@Qualifier("trainCache")
	private TrainCache trainCache;

	@Autowired
	private IOrderService orderService;

	@Autowired
	@Qualifier("strategyTrainCache")
	private StrategyTrainCache strategyTrainCache;


    /**
     * 校验
     */
	@Override
    public void check(DeleteTrainBo bo) throws DzcxException {
		if (bo.getTrainId() == null || bo.getTrainId() <=0l) {
			throw new DzcxException(ExceptionCode.车次id为空);
		}
		//判断该车次下是否有订单，给提示
		if (orderService.countByTrain(bo.getTrainId()) > 0) {
			throw new DzcxException(ExceptionCode.当前车次下有订单信息);
		}
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(DeleteTrainBo param) throws DzcxException {
    	Long trainId = param.getTrainId();
		DetailTrainVo vo =  trainRead.detail(trainId);

    	Train record = new Train();
    	record.setTrainId(trainId);
    	record.setDeleted(param.getDeleted());

    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	trainDao.delete(record);
    	param.setName(vo.getName());

		strategyTrainCache.clear(vo.getLineStrategyId());
    	trainCache.clear(trainId);
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(DeleteTrainBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.商品.key());
    	record.setAction(LogActionEnum.车次删除.key());
    	record.setTargetId(param.getTrainId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
